﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlueMarble.Genesis.Infrastructure.Security
{
    public class Profile
    {
        protected static ProfileServiceReference.ProfileService service;

        protected static Dictionary<string, object> GetAllPropertiesForCurrentUser(bool AuthenticatedUserOnly)
        {
            Profile.connectService();

            IAsyncResult asyncResult = Profile.service.BeginGetAllPropertiesForCurrentUser(AuthenticatedUserOnly, null, null);

            asyncResult.AsyncWaitHandle.WaitOne();

            return Profile.service.EndGetAllPropertiesForCurrentUser(asyncResult);
        }

#if !SILVERLIGHT
        protected static Dictionary<string, object> GetPropertiesForCurrentUser(string[] Properties, bool AuthenticatedUserOnly)
#else
        protected static Dictionary<string, object> GetPropertiesForCurrentUser(System.Collections.ObjectModel.ObservableCollection<string> Properties, bool AuthenticatedUserOnly)
#endif
        {
            Profile.connectService();

            IAsyncResult asyncResult = Profile.service.BeginGetPropertiesForCurrentUser(Properties, AuthenticatedUserOnly, null, null);

            asyncResult.AsyncWaitHandle.WaitOne();

            return Profile.service.EndGetPropertiesForCurrentUser(asyncResult);
        }

#if !SILVERLIGHT
        protected static ProfileServiceReference.ProfilePropertyMetadata[] GetPropertiesMetadata()
#else
        protected static System.Collections.ObjectModel.ObservableCollection<ProfileServiceReference.ProfilePropertyMetadata> GetPropertiesMetadata()
#endif
        {
            Profile.connectService();

            IAsyncResult asyncResult = Profile.service.BeginGetPropertiesMetadata(null, null);

            asyncResult.AsyncWaitHandle.WaitOne();

            return Profile.service.EndGetPropertiesMetadata(asyncResult);
        }

#if !SILVERLIGHT
        protected static string[] SetPropertiesForCurrentUser(Dictionary<string, object> Properties, bool AuthenticatedUserOnly)
#else
        protected static System.Collections.ObjectModel.ObservableCollection<string> SetPropertiesForCurrentUser(Dictionary<string, object> Properties, bool AuthenticatedUserOnly)
#endif
        {
            Profile.connectService();

            IAsyncResult asyncResult = Profile.service.BeginSetPropertiesForCurrentUser(Properties, AuthenticatedUserOnly, null, null);

            asyncResult.AsyncWaitHandle.WaitOne();

            return Profile.service.EndSetPropertiesForCurrentUser(asyncResult);
        }

        protected static void connectService()
        {
            if (Profile.service == null)
            {
                Profile.service = new ProfileServiceReference.ProfileServiceClient();
            }
        }
    }
}
